﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DataAccessLibrary;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class CustomerModule
    {
        private readonly CustomerDAO _custDAO = new CustomerDAO();

        public List<Customer> ReadAllCustomer()
        {
            return _custDAO.ReadAllCustomer();
        }

        public bool CreateCustomer(string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                     string pCustomerEmail, bool pCustomerSex, out string account)
        {
            account = "";
            var utility = new Utility();
            string err;
            var cust = new Customer
                           {
                               CustomerIdentifyNo = pIdentityNumber,
                               CustomerName = pCustomerName,
                               CustomerPhone = pCustomerPhone,
                               CustomerEmail = pCustomerEmail,
                               CustomerSex = pCustomerSex,
                               CustomerAccount = utility.CreateAccount(pCustomerName),
                               ModDay = DateTime.Today
                           };
            cust = FormatData(cust);
            if (ValidData(cust, out err))
            {
                return _custDAO.InsertCustomer(cust, out account);
            }
            throw new Exception(err);
        }

        public bool CustomerEmailCheck( string email)
        {
            return _custDAO.CheckCustomerEmail(email);
        }

        public bool CustomerIdCheck(string id)
        {
            return _custDAO.CheckCustomerId(id);
        }


        private bool ValidData(Customer customer, out string err)
        {
            int idxA = customer.CustomerEmail.IndexOf("@", StringComparison.Ordinal);
            if (idxA > -1)
            {
                if (customer.CustomerEmail.IndexOf(".", idxA, StringComparison.Ordinal) > idxA)
                {
                    var rePhone = new Regex(@"0+\d{2,3}-\d{3}-\d{4}");
                    if (rePhone.IsMatch(customer.CustomerPhone))
                    {
                        if (customer.CustomerName != "" && customer.CustomerEmail != "" &&
                            customer.CustomerPhone != "")
                        {
                            err = "";
                            return true;
                        }
                        err = "Null data";
                        return false;
                    }
                    err = "Phone number mus be valid format. \n" + "0xx[x]-xxx-xxxx";
                    return false;
                }
                err = "e-mail address must be valid e-mail address format.\nFor example 'someone@example.com' ";
                return false;
            }
            err = "Invalid data";
            return false;
        }

        private Customer FormatData(Customer customer)
        {
            customer.CustomerName = customer.CustomerName.Trim();
            customer.CustomerEmail = customer.CustomerEmail.Trim();
            customer.CustomerPhone = customer.CustomerPhone.Trim();
            customer.CustomerIdentifyNo = customer.CustomerIdentifyNo.Trim();
            return customer;
        }

        public bool EditCustomer(int pId,string pIdentityNumber, string pCustomerName, string pCustomerPhone,
                                   string pCustomerEmail, bool pCustomerSex)
        {
            string err;
            var cust = new Customer
                           {
                               CustomerID = pId,
                               CustomerIdentifyNo = pIdentityNumber,
                               CustomerName = pCustomerName,
                               CustomerPhone = pCustomerPhone,
                               CustomerEmail = pCustomerEmail,
                               CustomerSex = pCustomerSex,
                               ModDay = DateTime.Today
                           };
            cust = FormatData(cust);
            if (ValidData(cust, out err))
            {
                return _custDAO.UpdateCustomer(cust);
            }
            throw new Exception(err);
        }

        public List<Customer> FindCustomerByName(string pCustomerName)
        {
            return _custDAO.FindCustomerByName(pCustomerName);
        }

        public Customer FindCustomerByAccount(string pUn)
        {
            return _custDAO.FindExtUserByAccName(pUn);
        }

        public Customer FindCustomerByID(int id)
        {
            return _custDAO.FindCustomerByID(id);
        }

        public Customer FindExtUserByEmail(string pEmail)
        {
            return _custDAO.FindExtUserByEmail(pEmail);
        }
    }
}